package com.goldman.aplt.util;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.BeanWrapper;
import org.springframework.beans.BeanWrapperImpl;

import java.beans.PropertyDescriptor;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.function.Supplier;

/**
 * @Author: Tracy
 * @Date: 2023-11-09 14:06
 * @Description: Description
 */
@Slf4j
public class BeanCopyUtils {

    public static <T> T copyBean(Object sourceObject, Class<T> descClass) {
        if(sourceObject == null) {
            return null;
        }
        T descObject = null;
        try {
            descObject = descClass.newInstance();
            BeanUtils.copyProperties(sourceObject, descObject);
        } catch (InstantiationException | IllegalAccessException e) {
            log.error("对象copy异常，", e);
        }
        return descObject;
    }

    public static <T> T copyBean(Object sourceObject, Class<T> descClass, String... ignoreProperties) {
        if(sourceObject == null) {
            return null;
        }
        T descObject = null;
        try {
            descObject = descClass.newInstance();
            BeanUtils.copyProperties(sourceObject, descObject, ignoreProperties);
        } catch (InstantiationException | IllegalAccessException e) {
            log.error("对象copy异常，", e);
        }
        return descObject;
    }

    public static void copyBeanIgnoreNull(Object sourceObject, Object descObject) {
        BeanUtils.copyProperties(sourceObject, descObject, getNullPropertyNames(sourceObject));
    }

    private static String[] getNullPropertyNames (Object source) {
//        org.springframework.beans.BeanWrapper
        final BeanWrapper src = new BeanWrapperImpl(source);
//        java.beans.PropertyDescriptor
        PropertyDescriptor[] pds = src.getPropertyDescriptors();

        Set<String> emptyNames = new HashSet<>();
        for(PropertyDescriptor pd : pds) {
            Object srcValue = src.getPropertyValue(pd.getName());
            if (srcValue == null) {
                emptyNames.add(pd.getName());
            }
        }
        String[] result = new String[emptyNames.size()];
        return emptyNames.toArray(result);
    }


    /**
     * 集合数据的拷贝
     * @param sources: 数据源类
     * @param target: 目标类::new(eg: UserVO::new)
     * @return
     */
    public static <S, T> List<T> copyListProperties(List<S> sources, Supplier<T> target) {
        return copyListProperties(sources, target, null);
    }


    /**
     * 带回调函数的集合数据的拷贝（可自定义字段拷贝规则）
     * @param sources: 数据源类
     * @param target: 目标类::new(eg: UserVO::new)
     * @param callBack: 回调函数
     * @return
     */
    public static <S, T> List<T> copyListProperties(List<S> sources, Supplier<T> target, CommBeanUtil.BeanCopyUtilCallBack<S, T> callBack) {
        List<T> list = new ArrayList<>(sources.size());
        for (S source : sources) {
            T t = target.get();
            BeanUtils.copyProperties(source, t);
            list.add(t);
            if (callBack != null) {
                // 回调
                callBack.callBack(source, t);
            }
        }
        return list;
    }

    @FunctionalInterface
    public interface BeanCopyUtilCallBack <S, T> {

        /**
         * 定义默认回调方法
         * @param t
         * @param s
         */
        void callBack(S t, T s);
    }

}
